/**
 * Created by lixiaodi on 2018/10/10.
 */
mini.parse();
var form = new mini.Form("form1");
//得到集团/公司/部门
var nowGroups = mini.getByName("groups.groupId");
var nowCompanys = mini.getByName("company.companyId");
var nowDeps = mini.getByName("department.departmentId");
//是否操作员/操作员账号，操作员名字
var isOperator = mini.getByName("isOperator");
var operatorAccounts = mini.getByName("operatorAccounts");
var operatorName = mini.getByName("operatorName");
//当选择发生变化时
isOperator.on("checkedchanged",function(e){
   var nameCode = form.getData();
    if(isOperator.checked){
        operatorAccounts.setValue(nameCode.perCode);
        operatorName.setValue(nameCode.perName)
    }else{
        operatorAccounts.setValue("");
        operatorName.setValue("");
    }
});
//加载下拉框内容
$(function(){
    nowGroups.setUrl(ajaxUrl + "/GroupsController/getList.do");  //模块Url
    nowCompanys.setUrl(ajaxUrl + "/CompanyController/getList.do");  //模块Url
    nowDeps.setUrl(ajaxUrl + "/DepartmentController/getList.do");  //模块Url
});
function SaveData() {
    var ids = form.getData();
    if(ids.isOperator){
    	ids.isOperator = 1;
    }else{
    	ids.isOperator = 0;
    }
    var userInfo = {
        //人员基本信息
        perId : ids.perId,
        perCode: ids.perCode,
        perName : ids.perName,
        jobNumber: ids.jobNumber,
        //人员工作基本信息
        groupId : ids.groups.groupId,
        companyId : ids.company.companyId,
        departmentId : ids.department.departmentId,
        administration : ids.administration,
        costDep : ids.costDep,
        //postId : ids.postEntity,
        dutyId : ids.dutyEntity,
        arrivalDate : ids.arrivalDate,
        separationDate : ids.separationDate,
        sex : ids.sex,
        papersNo : ids.papersNo,
        isOperator : ids.isOperator,
        operatorAccounts : ids.operatorAccounts,
        operatorName : ids.operatorName,
        perPhone : ids.perPhone,
        email : ids.email,
        weChat : ids.weChat,
        age : ids.age,
        dateBirth : ids.dateBirth,
        //创建时间
        createDate : ids.createDate
    };
    console.log(userInfo);
    var jsonString = JSON.stringify(userInfo);

    //form.validate();
    //if (form.isValid() == false) return;

    $.ajax({
        url: ajaxUrl + "/personnerlUser/insertPersonnelUser.do",
        type: 'POST',
        data: jsonString,
        contentType : "application/json",
        success: function (data) {
            CloseWindow("save");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            CloseWindow();
        }
    });
}

//编辑
function SetData(data) {
    //跨页面传递的数据对象，克隆后才可以安全使用
    data = mini.clone(data);
    //判断状态是编辑还是新增
    if (data.action == "edit") {
        $.ajax({
            url: ajaxUrl + "/personnerlUser/findByPerId.do?perId=" + data.id,
            cache: false,
            success: function (data) {
                var o = mini.decode(data);
                form.setData(o.data);
                form.setChanged(false);
            }
        });
    }else if(data.action == "new"){
        var parsIds = data.parsId;
        //赋值
        nowGroups.setValue(parsIds.groupId);
        nowCompanys.setValue(parsIds.companyId);
        nowDeps.setValue(parsIds.depId);
    }
}

function GetData() {
    var o = form.getData();
    return o;
}
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
//////////////////////////////////
//        function onDeptChanged(e) {
//            var deptCombo = mini.getbyName("dept_id");
//            var positionCombo = mini.getbyName("position");
//            var dept_id = deptCombo.getValue();
//
//            positionCombo.load();
//            positionCombo.setValue("");
//        }

//重置
function onReset(){
    var ids = form.getData();
    var userId = ids.userId
    $.ajax({
        url: ajaxUrl + "/user/replacePassword.do?userId=" + userId,
        type:"post",
        success: function (data) {
            console.log(data)
        }
    });

}
